package org.mspring.mlog.web.module.web.plugin;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.mspring.mlog.core.ServiceFactory;
import org.mspring.mlog.utils.SkinUtils;
import org.mspring.mlog.web.module.web.Web_AbstractWidget;
import org.mspring.platform.utils.FreemarkerUtils;
import org.mspring.platform.web.freemarker.ExtendsFreeMarkerConfigurer;

import freemarker.template.Configuration;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月15日
 */
public class Plugin_AbstractWidget extends Web_AbstractWidget {
    private static ExtendsFreeMarkerConfigurer freeMarkerConfigurer;

    protected static Configuration getConfiguration(HttpServletRequest request) {
        if (freeMarkerConfigurer == null) {
            freeMarkerConfigurer = ServiceFactory.getBean(ExtendsFreeMarkerConfigurer.class);
        }
        return freeMarkerConfigurer.getConfiguration();
    }

    protected static String render(HttpServletRequest request, String template, Map<Object, Object> model) {
        Configuration configuration = getConfiguration(request);
        String templatePath = SkinUtils.getWidgetUrl(template);
        return FreemarkerUtils.render(configuration, templatePath, model);
    }
}
